<template>
  <div class="mainua">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white padding10 paddingTop0">
        <div class="main_ti box">
          <div class="back">
            <a href="javascript:;" @click="$router.back(-1)">
              <i class="t el-icon-arrow-left"></i>返回
            </a>
            <span>|</span>
          </div>
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="基础信息" name="1"></el-tab-pane>
            <el-tab-pane label="账期管理" name="2"></el-tab-pane>
            <el-tab-pane label="子账号" name="3"></el-tab-pane>
            <el-tab-pane label="开票信息" name="4"></el-tab-pane>
            <el-tab-pane label="常用商品" name="5"></el-tab-pane>
          </el-tabs>
        </div>
        <div class="mainua_form paddingTop10" v-if="activeName == 1">
          <el-form
            :inline="false"
            :rules="rules"
            :model="basic_form"
            class="demo-form-inline form-message-mg"
            ref="userListFrom"
            label-width="110px"
          >
            <el-form-item label="用户名称：" prop="nickname">
              <el-input
                v-model.trim="basic_form.nickname"
                placeholder="用户名称"
                clearable
                class="wid240"
                @input="basic_form.address.name = basic_form.nickname"
              ></el-input>
            </el-form-item>
            <el-form-item label="手机：" prop="mobile">
              <el-input
                v-model.trim="basic_form.mobile"
                placeholder="手机"
                clearable
                class="wid240"
                @input="basic_form.address.mobile = basic_form.mobile"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码：" style="margin-right: 0">
              <el-input
                v-model.trim="basic_form.password"
                show-password
                placeholder="不填为默认密码"
                clearable
                class="wid240"
              ></el-input>
            </el-form-item>
            <el-form-item label="用户分组：" prop="info.group_id">
              <el-select
                v-model.trim="basic_form.info.group_id"
                placeholder="用户分组"
                class="wid240"
              >
                <el-option label="无" :value="0"></el-option>
                <el-option
                  v-for="(group, index) in info.group"
                  :key="index"
                  :label="group"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="支付方式：" prop="info.pay_type">
              <el-select
                v-model.trim="basic_form.info.pay_type"
                placeholder="支付方式"
                class="wid240"
              >
                <el-option label="全部" :value="2"></el-option>
                <el-option label="货到付款" :value="0"></el-option>
                <el-option label="在线支付" :value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="状态：" prop="status">
              <el-select
                v-model.trim="basic_form.status"
                placeholder="状态"
                class="wid240"
              >
                <el-option label="禁用" :value="0"></el-option>
                <el-option label="正常" :value="1"></el-option>
                <el-option label="审核" :value="2"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="编码：" prop="info.code">
              <el-input
                v-model.trim="basic_form.info.code"
                placeholder="编码"
                clearable
                class="wid240"
              ></el-input>
              <el-input
                v-model.trim="prefix"
                placeholder="前缀(可以为空)"
                clearable
                class="wid140 marginLeft5"
              ></el-input>
              <el-button type="primary" class="marginLeft5" @click="automatic"
                >自动生成</el-button
              >
            </el-form-item>
            <el-form-item label="业务员：" prop="info.salesman_id">
              <el-select
                v-model.trim="basic_form.info.salesman_id"
                placeholder="业务员"
                class="wid240"
              >
                <el-option
                  v-for="(salesman, index) in info.salesman"
                  :key="index"
                  :label="salesman"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <!-- <el-form-item
              label="运费："
              prop="delivery_fee"
              style="margin-right: 0"
              :rules="rules.delivery_fee"
            >
              <el-input
                v-model.trim="basic_form.info.delivery_fee"
                placeholder="运费"
                @change="delivery_fee"
                clearable
                class="wid240"
              ></el-input>
            </el-form-item> -->

            <!-- <div class="box">
              <el-form-item
                label="查看价格："
                prop="info.is_look"
                label-width="130px"
              >
                <el-switch
                  v-model="basic_form.info.is_look"
                  active-color="#03A98E"
                  active-text="开"
                  inactive-text="关"
                  class="mainor_switch"
                ></el-switch>
              </el-form-item>
            </div> -->

            <div>
              <el-form-item
                label="用户标签："
                prop="info.labels"
                class="label_ids boxha"
              >
                <el-checkbox-group v-model="basic_form.info.labels">
                  <el-checkbox
                    v-for="(item, index) in info.labels"
                    :key="index"
                    :label="item.id"
                    :value="item.label"
                    >{{ item.label }}</el-checkbox
                  >
                </el-checkbox-group>
              </el-form-item>
            </div>

            <el-form-item label="查看价格：" prop="info.is_look">
              <el-switch
                v-model="basic_form.is_look"
                active-color="#03A98E"
                active-text="开"
                inactive-text="关"
                class="mainor_switch"
                :active-value="1"
                :inactive-value="0"
              ></el-switch>
            </el-form-item>

            <div class="main_tile boxdq">
              <div class="main_tile_l"><em></em>收货信息</div>
            </div>

            <el-form-item
              label="区域："
              prop="info.region_id"
              :rules="rules.region_id"
            >
              <el-select
                v-model.trim="basic_form.info.region_id"
                placeholder="区域"
                class="wid240"
              >
                <el-option
                  v-for="(region, index) in info.region"
                  :key="index"
                  :label="region"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="线路 ：" prop="info.line_id">
              <el-select
                v-model.trim="basic_form.info.line_id"
                placeholder="线路"
                class="wid240"
              >
                <el-option label="无" :value="0"></el-option>
                <el-option
                  v-for="(line, index) in info.line"
                  :key="index"
                  :label="line"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="联系人："
              prop="address.name"
              :rules="rules.name"
            >
              <el-input
                v-model.trim="basic_form.address.name"
                placeholder="联系人"
                clearable
                class="wid240"
              ></el-input>
            </el-form-item>
            <el-form-item
              label="收货人手机："
              prop="address.mobile"
              :rules="rules.mobile"
            >
              <el-input
                v-model.trim="basic_form.address.mobile"
                placeholder="收货人手机"
                clearable
                class="wid240"
              ></el-input>
            </el-form-item>
            <el-form-item label="配送时间：" prop="info.send_time_id ">
              <el-select
                v-model.trim="basic_form.info.send_time_id"
                placeholder="配送时间"
                class="wid240"
              >
                <el-option label="所有" :value="0"></el-option>
                <el-option
                  v-for="(send_time, index) in info.send_time"
                  :key="index"
                  :label="`${send_time.start_time}~${send_time.end_time}`"
                  :value="send_time.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="收货地址："
              prop="address.address_name"
              :rules="rules.address"
              class="shipping"
            >
              <div @click="shipping">
                <el-input
                  v-model.trim="basic_form.address.address_name"
                  placeholder="收货地址"
                  clearable
                  class="wid440"
                  suffix-icon="el-icon-location-outline"
                  :readonly="true"
                ></el-input>
              </div>
            </el-form-item>
            <el-form-item label="门牌号：" prop="address.detail">
              <el-input
                v-model.trim="basic_form.address.detail"
                placeholder="门牌号"
                clearable
                class="wid440"
              ></el-input>
            </el-form-item>
          </el-form>
          <div class="clearfix"></div>
          <div class="newConBox_p100 paddingBottom10" id="newConBox">
            <div class="newConBox_h32">
              <div
                :class="[
                  'mainua_an newConBox ',
                  { on: searchBarFixed },
                  { marginLeft5: !searchBarFixed },
                ]"
              >
                <el-button type="primary" @click="preservation" v-repeatClick
                  >保存</el-button
                >
                <el-button @click="$router.back(-1)">取消</el-button>
              </div>
            </div>
          </div>

          <currency-map
            ref="map"
            :zcenter="center"
            @setupmine_map="setupmine_map"
          ></currency-map>
        </div>
        <div class="mainua_form paddingTop10" v-if="activeName == 2">
          <div style="width: 90%">
            <el-form
              :inline="false"
              :rules="rules"
              :model="account_form"
              class="demo-form-inline form-message-mg"
              ref="userListFrom"
              label-width="110px"
            >
              <div class="boxdq">
                <div>
                  <el-form-item label="时间账期：" prop="overdue_status">
                    <el-switch
                      v-model="account_form.overdue_status"
                      active-color="#03A98E"
                      active-text="开"
                      inactive-text="关"
                      class="mainor_switch"
                    ></el-switch>
                  </el-form-item>
                  <el-form-item label="账期周期：" prop="overdue_days">
                    <el-input
                      v-model.trim="account_form.overdue_days"
                      placeholder="账期周期"
                      clearable
                      class="wid240"
                    ></el-input>
                    <span class="mainua_form_sp">天</span>
                  </el-form-item>
                  <el-form-item label="账期预警：" prop="warn_days">
                    <el-input
                      v-model.trim="account_form.warn_days"
                      placeholder="账期预警"
                      clearable
                      class="wid240"
                    ></el-input>
                    <span class="mainua_form_sp">天</span>
                  </el-form-item>
                  <div class="mainul_p">
                    <h3>功能说明：</h3>
                    <p>
                      1、账期周期：从用户第一个未支付订单开始，至周期最后一天
                    </p>
                    <p>
                      2、账期预警：从用户第一个未支付订单开始，至达到预警天数，用户自主提交、后台创建订单都会收到账期预警提醒
                    </p>
                    <p>
                      3、逾期：从用户第一个未支付订单开始，超过账期周期天数，用户自主提交、后台都无法创建订单
                    </p>
                  </div>
                </div>
                <div class="paddingLeft20">
                  <el-form-item label="信用账期：" prop="credit_status">
                    <el-switch
                      v-model="account_form.credit_status"
                      active-color="#03A98E"
                      active-text="开"
                      inactive-text="关"
                      v-if="info.credit_status == 1"
                      class="mainor_switch"
                    ></el-switch>
                  </el-form-item>
                  <el-form-item label="信用金额：" prop="credit">
                    <el-input
                      v-model.trim="account_form.credit"
                      placeholder="信用金额"
                      clearable
                      class="wid240"
                    ></el-input>
                    <span class="mainua_form_sp">元</span>
                  </el-form-item>

                  <div class="mainul_p" style="padding-top: 40px">
                    <h3>注意事项：</h3>
                    <p>
                      1、当客户欠款额度超过设置信用金额预警百分比时客户下单进行提示
                    </p>
                    <p>2、当客户欠款额度超过信用金额时客户不能进行下单业务</p>
                    <p>
                      3、欠款是指客户在平台使用货到付款下单的单据，包含待发货/待收货/已完成（但是未支付）的单据金额
                    </p>
                  </div>
                </div>
              </div>
            </el-form>
          </div>

          <div class="clearfix"></div>
          <div class="newConBox_p100 paddingBottom10" id="newConBox">
            <div class="newConBox_h32">
              <div
                :class="[
                  'mainua_an newConBox ',
                  { on: searchBarFixed },
                  { marginLeft5: !searchBarFixed },
                ]"
              >
                <el-button type="primary" @click="preservation" v-repeatClick
                  >保存</el-button
                >
                <el-button @click="$router.back(-1)">取消</el-button>
              </div>
            </div>
          </div>
        </div>
        <div class="paddingTop10" v-if="activeName == 3">
          <div class="mainor_form">
            <el-form
              :inline="true"
              :model="form"
              class="demo-form-inline"
              ref="userListFrom"
              label-width="60px"
            >
              <el-form-item label="名称：" prop="name">
                <el-input
                  v-model.trim="form.name"
                  placeholder="名称/手机"
                  clearable
                  class="wid220"
                ></el-input>
              </el-form-item>
              <el-form-item label="状态：" prop="status">
                <el-select
                  v-model.trim="form.status"
                  placeholder="审核状态"
                  class="wid220"
                >
                  <el-option label="所有" :value="0"></el-option>
                  <el-option label="禁用" :value="1"></el-option>
                  <el-option label="正常" :value="2"></el-option>
                </el-select>
              </el-form-item>
              <MainFormBtn @reset="reset_son" @getSearchFor="preservation_se" :loading="listLoading" :showBtn="false" />
            </el-form>
          </div>
          <div class="main-controls-wrap">
            <div>
              <el-button
                class="mainor_tea"
                @click="(addedFormVisible = true), (type = 0)"
                >创建子账号</el-button
              >
            </div>
          </div>
          <div class="main_table" style="min-height: 630px">
            <el-table
              ref="multipleTable"
              v-loading="listLoading"
              :data="list"
              element-loading-text="加载中"
              fit
              border
              highlight-current-row
            >
              <el-table-column
                label="名称"
                prop="nickname"
                width="150"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="手机"
                prop="mobile"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="联系人"
                prop="address.name"
                :show-overflow-tooltip="true"
              />
              <el-table-column label="收货地址" :show-overflow-tooltip="true">
                <template slot-scope="scope">{{
                  scope.row.address.address + scope.row.address.detail
                }}</template>
              </el-table-column>
              <el-table-column
                label="状态"
                prop="userNickname"
                width="150"
                :show-overflow-tooltip="true"
              >
                <template slot-scope="scope">
                  <div class="main_link">
                    <el-link type="success" v-if="scope.row.status == 1"
                      >正常</el-link
                    >
                    <el-link type="danger" v-else>禁用</el-link>
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                label="管理操作"
                align="center"
                fixed="right"
                width="150"
                class-name="main_hei"
              >
                <template slot-scope="scope">
                  <el-link
                    type="primary"
                    @click="
                      (addedFormVisible = true),
                        (type = 1),
                        (id = scope.row.id),
                        edit_assig(scope.$index)
                    "
                    >编辑</el-link
                  >
                  <el-link
                    type="danger"
                    @click="delet(scope.$index, scope.row.id)"
                    >删除</el-link
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="main_page boxdq">
            <div></div>
            <!-- 分页 -->
            <div class="main_page_r">
              <MainPagination
                :current-page="current.page"
                :page-sizes="[20, 30, 50, 100]"
                :page-size="limit"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totalCount"
                background
                @size-change="changeLimit"
                @current-change="changePage"
              />
            </div>
          </div>

          <el-dialog
            :title="type == 1 ? '编辑子账号' : '新增子账号'"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            @close="close"
            :visible.sync="addedFormVisible"
            class="main_dialog dialog450"
          >
            <el-form
              :inline="false"
              :model="billing_info"
              :rules="rules"
              class="demo-form-inline form-message-mg"
              ref="userListFrom"
              label-width="100px"
            >
              <el-form-item label="名称：" prop="nickname">
                <el-input
                  v-model.trim="billing_info.nickname"
                  placeholder="名称"
                  clearable
                  class="wid240"
                  @input="billing_info.address.name = billing_info.nickname"
                ></el-input>
              </el-form-item>
              <el-form-item label="手机：" prop="mobile">
                <el-input
                  v-model.trim="billing_info.mobile"
                  placeholder="手机"
                  clearable
                  class="wid240"
                  @input="billing_info.address.mobile = billing_info.mobile"
                ></el-input>
              </el-form-item>
              <el-form-item label="密码：" :prop="type == 1 ? '' : 'password'">
                <el-input
                  v-model.trim="billing_info.password"
                  show-password
                  :placeholder="
                    type == 1 ? '不填为默认密码' : '填写6位以上的密码'
                  "
                  clearable
                  class="wid240"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="联系人："
                prop="address.name"
                :rules="rules.name"
              >
                <el-input
                  v-model.trim="billing_info.address.name"
                  placeholder="联系人"
                  clearable
                  class="wid240"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="收货地址："
                prop="address.address"
                :rules="rules.address"
              >
                <el-input
                  v-model.trim="billing_info.address.address"
                  placeholder="收货地址"
                  clearable
                  class="wid240"
                ></el-input>
              </el-form-item>
              <el-form-item label="门牌号：" prop="address.detail">
                <el-input
                  v-model.trim="billing_info.address.detail"
                  placeholder="门牌号"
                  clearable
                  class="wid240"
                ></el-input>
              </el-form-item>
              <el-form-item label="状态：" prop="status">
                <el-switch
                  v-model="billing_info.status"
                  active-color="#03A98E"
                  active-text="开"
                  inactive-text="关"
                  class="mainor_switch"
                ></el-switch>
              </el-form-item>
              <el-form-item label="查看价格：" prop="is_look">
                <el-switch
                  v-model="billing_info.is_look"
                  active-color="#03A98E"
                  active-text="开"
                  inactive-text="关"
                  class="mainor_switch"
                ></el-switch>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="(addedFormVisible = false), close()"
                >取 消</el-button
              >
              <el-button type="primary" @click="setupmine" v-repeatClick
                >确 定</el-button
              >
            </div>
          </el-dialog>
        </div>
        <div class="mainua_form paddingTop10" v-if="activeName == 4">
          <el-form
            :inline="false"
            :rules="rules"
            :model="billing_form"
            class="demo-form-inline form-message-mg"
            ref="userListFrom"
            label-width="110px"
          >
            <el-form-item label="抬头类型：" prop="type">
              <el-select
                v-model.trim="billing_form.type"
                placeholder="抬头类型"
                class="wid240"
                @change="change_billing_form_type"
              >
                <el-option label="个人或事业单位" :value="1"></el-option>
                <el-option label="企业" :value="2"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="抬头：" prop="title">
              <el-input
                v-model.trim="billing_form.title"
                placeholder="抬头"
                clearable
                class="wid240"
              ></el-input>
            </el-form-item>
            <div v-if="isShow">
              <el-form-item label="税号：" prop="tax_code">
                <el-input
                  v-model.trim="billing_form.tax_code"
                  placeholder="税号"
                  clearable
                  class="wid240"
                ></el-input>
              </el-form-item>
              <el-form-item label="开户银行：" prop="bank">
                <el-input
                  v-model.trim="billing_form.bank"
                  placeholder="开户银行"
                  clearable
                  class="wid240"
                ></el-input>
              </el-form-item>
              <el-form-item label="银行账号：" prop="bank_account">
                <el-input
                  v-model.trim="billing_form.bank_account"
                  placeholder="银行账号"
                  clearable
                  class="wid240"
                ></el-input>
              </el-form-item>
              <el-form-item label="地址：" prop="address_new">
                <el-input
                  v-model.trim="billing_form.address"
                  placeholder="地址"
                  clearable
                  class="wid240"
                ></el-input>
              </el-form-item>
              <el-form-item label="电话：" prop="mobile_new">
                <el-input
                  v-model.trim="billing_form.mobile"
                  placeholder="电话"
                  clearable
                  class="wid240"
                ></el-input>
              </el-form-item>
            </div>
          </el-form>
          <div class="clearfix"></div>
          <div class="newConBox_p100 paddingBottom10" id="newConBox">
            <div class="newConBox_h32">
              <div
                :class="[
                  'mainua_an newConBox ',
                  { on: searchBarFixed },
                  { marginLeft5: !searchBarFixed },
                ]"
              >
                <el-button type="primary" @click="preservation" v-repeatClick
                  >保存</el-button
                >
                <el-button @click="$router.back(-1)">取消</el-button>
              </div>
            </div>
          </div>
        </div>
        <div class="mainua_form" v-if="activeName == 5">
          <userCommonGoods :id="user_id"></userCommonGoods>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  member_add,
  member_edit,
  member_base,
  member_generateCode,
  member_info,
  member_accountPeriod,
  member_saveAccountPeriod,
  member_invoice,
  member_saveInvoice,
  member_subAccount,
  member_delete,
} from "@/api/user/user";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import rules from "@/rules/user/userList";
import currencyMap from "@/components/public/map";
import userCommonGoods from "@/views/user/components/userCommonGoods";
export default {
  components: {
    currencyMap,
    userCommonGoods,
  },
  computed: {
    // 开票信息
    isShow() {
      if (this.billing_form.type == 1) {
        return false;
      } else {
        return true;
      }
    },
  },
  data() {
    return {
      // 地图
      center: {
        lng: 115.89352755,
        lat: 28.689578,
      },
      // 增加和编辑
      billing_info: {
        pid: this.$route.params.id,
        nickname: "",
        mobile: "",
        password: "",
        status: true,
        is_look: true,
        address: {
          name: "",
          mobile: "",
          id: "",
          address: "",
          detail: "",
        },
      },
      type: 0,
      id: 0,
      addedFormVisible: false,
      // 验证
      rules,
      // 搜索
      form: {
        name: "",
        status: "",
      },
      basic_form: {
        pid: 0,
        nickname: "",
        mobile: "",
        password: "",
        status: 1,
        address: {
          name: "",
          id: "",
          mobile: "",
          address: "",
          address_name: "",
          detail: "",
        },
        info: {
          delivery_fee: "",
          send_time_id: 1,
          group_id: null,
          id: "",
          salesman_id: null,
          pay_type: 0,
          code: "",
          // is_look: true,
          line_id: null,
          region_id: "",
          labels: [], //用户标签数组
        },
        is_look: 1, // 显示价格 1显示 0隐藏
        lat: "",
        lng: "",
      },
      account_form: {
        overdue_status: false,
        overdue_days: "",
        warn_days: "",
        credit_status: false,
        credit: "",
      },
      billing_form: {
        type: "",
        title: "",
        tax_code: "",
        bank: "",
        bank_account: "",
        address: "",
        mobile: "",
      },
      prefix: "",
      // 信息
      info: {},
      // 监听滚动
      searchBarFixed: false,
      // 选中菜单
      activeName: "1",
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: [],
      listLoading: false,
      // id
      user_id: this.$route.params.id,
    };
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll);
  },
  destroyed() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  created() {
    // 下拉信息
    this.member_base().then(() => {
      // 基本信息
      this.member_info();
    });

    // 账期管理
    this.member_accountPeriod();
    // 子账号
    this.member_subAccount();
    // 开票信息
    this.member_invoice();
  },
  methods: {
    // 选中菜单
    handleClick(tab, event) {
      this.searchBarFixed = false;
      if (this.$refs["userListFrom"]) {
        this.$refs["userListFrom"].clearValidate();
      }
    },
    // 地图
    shipping() {
      this.center.lng = this.basic_form.lng
        ? this.basic_form.lng
        : 115.89352755;
      this.center.lat = this.basic_form.lat ? this.basic_form.lat : 28.689578;
      this.$refs.map.sing();
    },
    setupmine_map(data) {
      this.basic_form.lng = data.center.lng;
      this.basic_form.lat = data.center.lat;
      this.basic_form.address.address = data.address;
      if (!data.addressStandby) {
        this.basic_form.address.address = data.address;
        this.basic_form.address.address_name = data.address;
      } else {
        this.basic_form.address.address = data.addressStandby;
        this.basic_form.address.address_name = data.address;
      }
      this.$refs["userListFrom"].clearValidate();
    },
    // 监听滚动
    handleScroll() {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      if (document.getElementById("newConBox")) {
        scrollTop + document.documentElement.clientHeight >
        document.getElementById("newConBox").offsetTop + 200
          ? (this.searchBarFixed = false)
          : (this.searchBarFixed = true);
      } else {
        this.searchBarFixed = true;
      }
    },
    // 子账号关闭清空表单
    close() {
      this.billing_info = {
        pid: this.$route.params.id,
        nickname: "",
        mobile: "",
        password: "",
        status: true,
        is_look: true,
        address: {
          name: "",
          id: "",
          address: "",
          detail: "",
        },
      };
      this.$refs["userListFrom"].clearValidate();
    },
    delivery_fee(val) {
      if (!/^(\d+\.?)?\d{0,2}$/.test(val)) {
        this.basic_form.info.delivery_fee = 0;
      }
    },
    // 子账号删除
    delet(index, id) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: id }
      );
      this.$confirm("是否删除子账号?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          member_delete(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.current.page = 1;
              this.list.splice(index, 1);
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
        .catch(() => {});
    },
    // 子账号确认
    setupmine() {
      this.$refs["userListFrom"].validate((valid) => {
        if (valid) {
          if (this.type == 1) {
            if (
              this.billing_info.password != "" &&
              this.billing_info.password.length < 6
            ) {
              this.$message.error("密码为6位以上");
              return;
            }
          }
          this.type == 0 ? this.add() : this.edit();
        }
      });
    },
    // 子账号编辑赋值
    edit_assig(index) {
      this.billing_info = {
        pid: this.$route.params.id,
        nickname: this.list[index].nickname,
        mobile: this.list[index].mobile,
        password: "",
        status: this.list[index].status == 1 ? true : false,
        is_look: this.list[index].is_look == 1 ? true : false,
        address: {
          name: this.list[index].address.name,
          mobile: this.list[index].address.mobile,
          id: this.list[index].address.id,
          address: this.list[index].address.address,
          detail: this.list[index].address.detail,
        },
      };
    },
    // 子账号新增保存
    add() {
      let billing_info = JSON.parse(JSON.stringify(this.billing_info));
      let { id, ...params } = billing_info.address;
      billing_info.status = billing_info.status ? 1 : 0;
      billing_info.is_look = billing_info.is_look ? 1 : 0;
      billing_info.address = params;
      billing_info.address = JSON.stringify(billing_info.address);
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        billing_info
      );
      member_add(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.current.page = 1;
          this.member_subAccount();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 子账号编辑保存
    edit() {
      let billing_info = JSON.parse(JSON.stringify(this.billing_info));
      billing_info.address = JSON.stringify(billing_info.address);
      billing_info.status = billing_info.status ? 1 : 0;
      billing_info.is_look = billing_info.is_look ? 1 : 0;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id },
        billing_info
      );
      member_edit(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message.success("编辑成功");
          this.member_subAccount();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 子账号搜索
    preservation_se() {
      this.current.page = 1;
      this.member_subAccount();
    },
    //子账号重置
    reset_son() {
      this.$refs["userListFrom"].resetFields();
      this.member_subAccount();
    },
    // 子账号分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.member_subAccount();
    },
    changePage(page) {
      this.current.page = page;
      this.member_subAccount();
    },
    // 自动生成
    automatic() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { prefix: this.prefix })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { prefix: this.prefix }
      );
      member_generateCode(data).then((res) => {
        if (res.errcode == 0) {
          this.basic_form.info.code = this.prefix + res.data.codeNumber;
          this.$refs["userListFrom"].clearValidate();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 保存
    preservation() {
      this.$refs["userListFrom"].validate((valid) => {
        if (valid) {
          // 基础信息
          let basic_form = JSON.parse(JSON.stringify(this.basic_form));

          basic_form.info.line_id = basic_form.info.line_id
            ? basic_form.info.line_id
            : 0;
          basic_form.info.salesman_id = basic_form.info.salesman_id
            ? basic_form.info.salesman_id
            : 0;
          // basic_form.info.is_look = basic_form.info.is_look ? 1 : 0;
          basic_form.info.group_id = basic_form.info.group_id
            ? basic_form.info.group_id
            : 0;
          basic_form.info.labels =
            basic_form.info.labels.length != 0
              ? basic_form.info.labels.join(",")
              : "";
          basic_form.info.region_id = basic_form.info.region_id
            ? basic_form.info.region_id
            : 0;
          basic_form.address = JSON.stringify(basic_form.address);
          basic_form.info = JSON.stringify(basic_form.info);
          // 账期管理
          let account_form = JSON.parse(JSON.stringify(this.account_form));
          account_form.overdue_status = account_form.overdue_status ? 1 : 0;
          account_form.credit_status = account_form.credit_status ? 1 : 0;
          // 开票信息
          let billing_form = JSON.parse(JSON.stringify(this.billing_form));

          let data = Object.assign(
            {
              sign: Md5(
                objKeySort(
                  Object.assign(
                    apiUrl().name,
                    this.activeName == 1
                      ? { id: this.user_id }
                      : { user_id: this.user_id }
                  )
                ) + apiUrl().appsecret
              ),
            },
            apiUrl().name,
            this.activeName == 1
              ? { id: this.user_id }
              : { user_id: this.user_id },
            this.activeName == 1
              ? basic_form
              : this.activeName == 2
              ? account_form
              : billing_form
          );
          if (this.activeName == 1) {
            if (
              this.basic_form.password != "" &&
              this.basic_form.password.length < 6
            ) {
              this.$message.error("密码为6位以上");
              return;
            }
            member_edit(data).then((res) => {
              if (res.errcode == 0) {
                this.$message.success("编辑成功");
              } else {
                this.$message.error(res.errmsg);
              }
            });
          } else if (this.activeName == 2) {
            member_saveAccountPeriod(data).then((res) => {
              if (res.errcode == 0) {
                this.$message.success("编辑成功");
              } else {
                this.$message.error(res.errmsg);
              }
            });
          } else {
            member_saveInvoice(data).then((res) => {
              if (res.errcode == 0) {
                this.$message.success("编辑成功");
              } else {
                this.$message.error(res.errmsg);
              }
            });
          }
        }
      });
    },
    // 下拉信息
    member_base() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name
      );
      return new Promise((resolv, reject) => {
        member_base(data).then((res) => {
          if (res.errcode == 0) {
            this.info = res.data;
          } else {
            this.$message.error(res.errmsg);
          }
          resolv();
        });
      });
    },
    // 基本信息
    member_info() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.user_id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.user_id }
      );
      member_info(data).then((res) => {
        if (res.errcode == 0) {
          this.basic_form.nickname = res.data.member.nickname;
          this.basic_form.mobile = res.data.member.mobile;
          this.basic_form.status = parseInt(res.data.member.status);
          this.basic_form.address.address = res.data.member.address.address;
          this.basic_form.address.id = res.data.member.address.id;
          this.basic_form.address.name = res.data.member.address.name;
          this.basic_form.address.mobile = res.data.member.address.mobile;
          this.basic_form.address.address_name =
            res.data.member.address.address_name;
          this.basic_form.address.detail = res.data.member.address.detail;
          this.basic_form.info.id = res.data.member.info.id;

          this.basic_form.info.send_time_id = parseInt(
            res.data.member.info.send_time_id
          );
          this.basic_form.info.group_id = res.data.member.info.group_id
            ? res.data.member.info.group_id
            : null;
          let salesmanArry = Object.keys(this.info.salesman);
          salesmanArry = salesmanArry.map(Number);

          this.basic_form.info.salesman_id = res.data.member.info.salesman_id
            ? res.data.member.info.salesman_id
            : null;
          if (salesmanArry.indexOf(this.basic_form.info.salesman_id) == -1) {
            this.basic_form.info.salesman_id = null;
          }
          this.basic_form.info.pay_type = parseInt(
            res.data.member.info.pay_type
          );
          this.basic_form.info.code = res.data.member.info.code;
          // this.basic_form.info.is_look =
          //   res.data.member.info.is_look == 1 ? true : false;
          this.basic_form.info.line_id = res.data.member.info.line_id
            ? res.data.member.info.line_id
            : null;
          this.basic_form.is_look = res.data.member.is_look // 显示价格赋值
          this.info.labels = res.data.labels ? res.data.labels : [];
          this.basic_form.info.labels = res.data.member.info.labels
            ? res.data.member.info.labels.split(",").map((e) => parseInt(e))
            : [];

          // 筛选
          let arr2 = [];
          let labels_ids = this.info.labels.map((e) => {
            return e.id;
          });
          labels_ids.filter((item) => {
            this.basic_form.info.labels.includes(item) && arr2.push(item);
          });
          this.basic_form.info.labels = arr2;

          this.basic_form.info.delivery_fee = res.data.member.info.delivery_fee;
          if (res.data.member.info.region_id != 0) {
            this.basic_form.info.region_id = parseInt(
              res.data.member.info.region_id
            );
          } else {
            this.basic_form.info.region_id = "";
          }
          this.basic_form.lat = res.data.member.lat;
          this.basic_form.lng = res.data.member.lng;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 账期管理
    member_accountPeriod() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, { user_id: this.user_id })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { user_id: this.user_id }
      );
      member_accountPeriod(data).then((res) => {
        if (res.errcode == 0) {
          if (res.data.credit) {
            this.account_form.credit_status =
              res.data.credit.status == 1 ? true : false;
          }
          this.account_form.credit = res.data.credit
            ? res.data.credit.credit
            : this.info.credit;
          if (res.data.overdue) {
            this.account_form.overdue_status =
              res.data.overdue.status == 1 ? true : false;
            this.account_form.overdue_days = res.data.overdue.overdue_days;
            this.account_form.warn_days = res.data.overdue.warn_days;
          }
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 子账号
    member_subAccount() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, { pid: this.user_id }, this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { pid: this.user_id },
        this.current,
        JSON.parse(JSON.stringify(this.form))
      );
      member_subAccount(data).then((res) => {
        if (res.errcode == 0) {
          this.list = res.data.list;
          this.listLoading = false;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    change_billing_form_type(val) {
      this.member_invoice(val);
    },
    // 开票信息放
    member_invoice(type) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, { user_id: this.user_id })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { user_id: this.user_id, type: type }
      );
      return new Promise((resolve) => {
        member_invoice(data).then((res) => {
          if (res.errcode == 0) {
            if (type == 1) {
              this.billing_form.type = 1;
            } else {
              this.billing_form.type = 2;
            }

            if (res.data.info) {
              this.billing_form.type = parseInt(res.data.info.type);
              this.billing_form.title = res.data.info.title;
              this.billing_form.tax_code = res.data.info.tax_code;
              this.billing_form.bank = res.data.info.bank;
              this.billing_form.bank_account = res.data.info.bank_account;
              this.billing_form.address = res.data.info.address;
              this.billing_form.mobile = res.data.info.mobile;
            }
          } else {
            this.$message.error(res.errmsg);
          }
          resolve(res);
        });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";
/deep/.el-form-item--mini.el-form-item, .el-form-item--small.el-form-item{
  margin-bottom: 10px;
}
.mainor_form form {
  width: calc(100% - 120px);
}

.mainua_form {
  margin-left: 10px;
  margin-top: 10px;
  /deep/.el-form-item--mini.el-form-item,
  /deep/.el-form-item--small.el-form-item {
    margin-bottom: 12px;
  }
  .main_tile {
    margin-left: -10px;
  }
  .mainua_form_sp {
    margin-left: 2px;
  }
  .label_ids {
    display: block;
    /deep/.el-form-item__content {
      width: 50%;
      display: flex;
      flex-wrap: wrap;
    }
  }
  .shipping {
    /deep/.el-input__inner {
      background: #f5f7fa;
      &:hover {
        cursor: pointer;
      }
    }
    /deep/.el-input__icon {
      font-size: $font_18;
      color: $colour_6;
    }
  }
  .mainua_an {
    /deep/.el-form-item__content {
      margin-left: 0px !important;
    }
    .reset {
      background: $subActive;
      color: $menuText;
      border-color: $subActive;

      &:hover,
      &:focus {
        border-color: $menuActiveText;
        color: $menuActiveText;
      }
    }
  }
}
.main_ti {
  .back {
    padding: 0 55px 0 0;
    font-size: 14px;
    color: #03a98e;
    display: flex;
    align-items: center;
    flex-direction: row;
    font-size: 0;
    a {
      font-size: 14px;
      color: #03a98e;
    }
    span {
      font-size: 14px;
      padding: 0 14px;
      transform: translateY(-1px);
    }
  }
  border-bottom: 1px solid #e4e7ed;
  /deep/.el-tabs__nav-wrap::after {
    background: none;
  }
  /deep/.el-tabs__header {
    margin: 0;
  }
}
.mainul_p {
  margin-left: 30px;
  margin-top: 40px;
  h3 {
    color: $colour_18;
    font-size: $font_14;
    font-weight: bold;
    margin-bottom: 20px;
  }
  p {
    color: $colour_18;
    font-size: $font_14;
    margin-top: 10px;
  }
}

.serachinput {
  font-size: $font_14;
  color: $colour_6;
  -webkit-appearance: none;
  background-color: #fff;
  background-image: none;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  display: inline-block;
  height: 32px;
  line-height: 32px;
  outline: 0;
  padding: 0 15px;
  -webkit-transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  width: 100%;
  &:focus {
    border-color: $menuActiveText;
  }
  &:active {
    border-color: $menuActiveText;
  }
}
#allmap {
  height: 400px;
}
</style>
